﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ttbit.core;
using ttbit.WorkingSpace;

namespace pixeleditor.WorkingSpace.Resource
{
    public class TSpriteData
    {
        //此处需要一个Resource的关联机制，
        //public string atlas;
        //public bool inPack;
        public Rectangle uv;
        public Point povit;
        public int Width
        {
            get
            {
                return uv.Width;

            }
        }
        public int Height
        {
            get
            {
                return uv.Height;
            }
        }
        public void WriteToJson(JObject json)
        {
            //json["atlas"] = atlas;
            //json["inpack"] = inPack;
            json["x"] = uv.X;
            json["y"] = uv.Y;
            json["w"] = uv.Width;
            json["h"] = uv.Height;
            json["px"] = povit.X;
            json["py"] = povit.Y;
        }
        public void ReadFromJson(JObject json)
        {
            //if (json.ContainsKey("atlas"))
            //    this.atlas = json["atlas"].ToString();
            //else
            //    this.atlas = null;
            //this.inPack = (bool)json["inpack"];
            this.uv.X = (int)json["x"];
            this.uv.Y = (int)json["y"];
            this.uv.Width = (int)json["w"];
            this.uv.Height = (int)json["h"];
            this.povit.X = (int)json["px"];
            this.povit.Y = (int)json["py"];
        }

    }

    public class Resource_Sprite : IWorkResource
    {
        public const string TypeStr = "Sprite";

        public Resource_Sprite(string name)
        {
            this.sprite = new TSpriteData();
            this.Name = name;
        }
        public string Name
        {
            get;
            private set;
        }
        public string[] RefResource => null;

        public bool ContainsResource(string name) => false;

        public TSpriteData sprite;
        public Texture textureStandalone;
        public string TypeName => TypeStr;
        public Rectangle uv
        {
            get
            {
                return this.sprite.uv;
            }
            set
            {
                this.sprite.uv = value;
                MarkDirty();
            }
        }
        public Point Povit
        {
            get
            {
                return this.sprite.povit;
            }
            set
            {
                this.sprite.povit = value;
                MarkDirty();
            }
        }

        public bool Dirty
        {
            get;
            private set;
        }
        public void MarkDirty()
        {
            Dirty = true;
        }
        public void ClearDirty()
        {
            Dirty = false;
        }
        public void Dispose()
        {

        }
    }
}
